﻿@model ElectrosLtdApplication.Models.LogEntriesModel 

@{
    ViewBag.Title = "Electros Ltd | Log Entries";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Log Entries</h2>
<script type="text/javascript">
    $(function () {
        $("#start").datepicker();
        $("#end").datepicker();
    });

    function sortbydate() {
        $.ajax({
            url: '@Url.Action("SortEntriesByDate")',
            type: "GET",
            datatype: "html",
            UpdateTargetId: "divResult",
            data: null,
            error: function (xmlHttpRequest, errorText, thrownError) {
                alert("An error has occurred.");
            },
            success: function (data) {
                $('#divResult').empty();
                $('#divResult').html(data);
            }
        });
    }

    function searchusername() {
        var client = $('#ddlUsers').val();

        if (client == "") {
            alert("Please choose a username!");
        }
        else {
            var param = { username: client };

            $.ajax({
                url: '@Url.Action("FilterByClientID")',
                type: "GET",
                datatype: "html",
                UpdateTargetId: "divResult",
                data: param,
                error: function (xmlHttpRequest, errorText, thrownError) {
                    alert("An error has occurred.");
                },
                success: function (data) {
                    $('#divResult').empty();
                    $('#divResult').html(data);
                }
            });
        }
    }

    function searchfaultid() {
        var fault = $('#ddlFaults').val();

        if (fault == "") {
            alert("Please choose a Fault ID!");
        }
        else {
            var param = { faultID: fault };

            $.ajax({
                url: '@Url.Action("FilterByFaultID")',
                type: "GET",
                datatype: "html",
                UpdateTargetId: "divResult",
                data: param,
                error: function (xmlHttpRequest, errorText, thrownError) {
                    alert("An error has occurred.");
                },
                success: function (data) {
                    $('#divResult').empty();
                    $('#divResult').html(data);
                }
            });
        }
    }

    function searchdate() {
        var startdate = $('#start').val();
        var enddate = $('#end').val();

        if ((startdate == "") && (enddate == "")) {
            alert("Please choose a start date and end date.");
        }
        else if (startdate == "") {
            alert("Please choose a start date");
        }
        else if (enddate == "") {
            alert("Please choose an end date");
        }
        else {
            var param = { startDate: startdate, endDate: enddate };

            $.ajax({
                url: '@Url.Action("FilterByLogDate")',
                type: "GET",
                datatype: "html",
                UpdateTargetId: "divResult",
                data: param,
                error: function (xmlHttpRequest, errorText, thrownError) {
                    alert("An error has occurred.");
                },
                success: function (data) {
                    $('#divResult').empty();
                    $('#divResult').html(data);
                }
            });
        }
    }

    function searchall() {
        var fault = $('#ddlFaults').val();
        var client = $('#ddlUsers').val();
        var startdate = $('#start').val();
        var enddate = $('#end').val();

        if ((startdate == "") && (enddate == "") && (fault == "") && (client == "")) {
            alert("Please choose a username, Fault ID, start date and end date!");
        }
        else if (fault == "") {
            alert("Please choose a Fault ID!");
        }
        else if (client == "") {
            alert("Please choose a username!");
        }
        else if (startdate == "") {
            alert("Please choose a start date!");
        }
        else if (enddate == "") {
            alert("Please choose an end date!");
        }
        else {
            var param = { faultID: fault, username: client, startDate: startdate, endDate: enddate };

            $.ajax({
                url: '@Url.Action("FilterAll")',
                type: "GET",
                datatype: "html",
                UpdateTargetId: "divResult",
                data: param,
                error: function (xmlHttpRequest, errorText, thrownError) {
                    alert("An error has occurred.");
                },
                success: function (data) {
                    $('#divResult').empty();
                    $('#divResult').html(data);
                }
            });
        }
    }
</script>

@Html.Label("Username: ")
@Html.DropDownListFor(model => Model.Users, Model.Users, "--Select Username--", new { id = "ddlUsers" }) <br />
<button type="button" onclick="searchusername()">Search by Username</button><br /> <br />

@Html.Label("Fault ID: ") 
@Html.DropDownListFor(model => model.FaultID, Model.Faults, "--Select Fault ID--", new { id = "ddlFaults" }) <br />
<button type="button" onclick="searchfaultid()">Search by FaultID</button><br /> <br />

@Html.Label("Start Date: ") <input type="text" id="start" /> <br />
@Html.Label("End Date: ") <input type="text" id="end" /> <br />

<button type="button" onclick="searchdate()">Search by Date</button><br /> <br />
<button type="button" onclick="searchall()">Search by All</button>
<button type="button" onclick="sortbydate()">Sort by Date</button><br /> <br />

<div id = "divResult"></div>